package com.qf.openapi2204.es.consumer;

import com.netflix.discovery.converters.Auto;
import com.qf.openapi2204.common.mqbean.ApiTimeCostMqBean;
import com.qf.openapi2204.es.esvo.ApiTimeCostEsVo;
import com.qf.openapi2204.es.repository.ApiTimeCostRepository;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class ApiTimeCostConsumer {

    @Autowired
    ApiTimeCostRepository apiTimeCostRepository;

    @RabbitListener(queues = "openapi-apitimecost-queue")
    public void getApiTimeCostMsg(ApiTimeCostMqBean mqBean, Channel channel, Message message) throws IOException {

//        System.out.println(mqBean);

        // 把消息写入es 索引中
        // ApiTimeCostMqBean ---》 ApiTimeCostEsVo
        ApiTimeCostEsVo apiTimeCostEsVo = new ApiTimeCostEsVo();
        BeanUtils.copyProperties(mqBean,apiTimeCostEsVo);
        apiTimeCostRepository.save(apiTimeCostEsVo);

        // 手动ack
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);

    }

}
